/**
 * 
 */
package com.gkwave.action.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.MessageBean;
import com.gkwave.module.bean.TransportRegisterBean;
import com.gkwave.module.dao.EmailDAO;
import com.gkwave.module.dao.TransportRegisterDAO;

/**
 * @author Administrator
 *
 */
public class TransportRegisterSubmitService extends BaseService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5784394658266577418L;

	/**
	 * 
	 */
	public TransportRegisterSubmitService() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.gkwave.action.servlet.BaseServlet#procesRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void procesRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	String idTransportService = request.getParameter("idTransportService");
    	String idRoute = request.getParameter("idRoute");
    	String idBoardingPoint = request.getParameter("idBoardingPoint");
    	String idUser = request.getParameter("idUser");
    	String nmMobileNumber = request.getParameter("nmMobileNumber");
    	
    	out=response.getWriter();
    	
    	if (idTransportService != null && idRoute != null && idUser != null && idBoardingPoint!= null) {
    		
    		TransportRegisterDAO dao = new TransportRegisterDAO();
    		
    		Integer id_TransportService = Integer.parseInt(idTransportService);
    		Integer id_Route = Integer.parseInt(idRoute);
    		Integer id_User = Integer.parseInt(idUser);
    		Integer id_boarding_point = Integer.parseInt(idBoardingPoint);
    		
    		MessageBean result = dao.doRegister(id_TransportService, 
    											id_Route, 
    											id_boarding_point, 
    											id_User, 
    											nmMobileNumber);
    		xmlResponse = result.marsel(result);
    		if(result.getStatus().equals("Success")){
    			Integer idTransportRegistration = null;
    			String nmTransportService = null;
    			String nmRoute = null;
    			String nmBoardingLocation = null;
    			String nmFullname = null;
    			String nmMobile = null;
    			String tsTripTime = null;
    			String idEmail = null;
    			try {
					TransportRegisterBean bean = dao.getRegistrationInfo(id_User);
	    			idTransportRegistration = bean.getIdTransportRegistration();
	    			nmTransportService=  bean.getNmTransportService();
	    			nmRoute =  bean.getNmRoute();
	    			nmBoardingLocation =  bean.getNmBoardingLocation();
	    			nmFullname =  bean.getNmFullname();
	    			nmMobile =  bean.getNmMobileNumber();
	    			tsTripTime =  bean.getTsTripTime();
	    			idEmail = bean.getIdEmail();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				StringBuffer messageContent  = new StringBuffer();
				messageContent.append("Transport Registration Details:");
				messageContent.append("\n--------------------------------\n\n");
				messageContent.append("Registration Number: " + idTransportRegistration);
				messageContent.append("\n");
				messageContent.append("Transport Service: " + nmTransportService);
				messageContent.append("\n");
				messageContent.append("Route Name: " + nmRoute);
				messageContent.append("\n");
				messageContent.append("Boarding Point: " + nmBoardingLocation);
				messageContent.append("\n");
				messageContent.append("Boarding Time: " + tsTripTime);
				messageContent.append("\n\n");
				messageContent.append("Passenger Details: ");
				messageContent.append("\n------------------\n");
				messageContent.append("Name: " + nmFullname);
				messageContent.append("\n");
				messageContent.append("Moble Number: " + nmMobile);
				messageContent.append("\n");
    			EmailDAO.sendEmailNotification("[SMARTC Notification] Transport Registration", messageContent.toString(), new String[]{idEmail});
    		}
    	} else{
    		xmlResponse = "Eror: Input is empty \nidTransportService = " + idTransportService + "\nidRoute="+idRoute+"\nidBoardingPoint =" + idBoardingPoint +"\nidUser="+idUser+"\nnmMobileNumber="+nmMobileNumber;
    	}
    	System.out.println(xmlResponse);
    	out.println(xmlResponse);
	}

}
